package com.chinaunicom.emergency.util;


import com.chinaunicom.emergency.exception.CheckException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;

/**
 * @author liliang
 * @date 2019-12-13
 */
public class Assert {

    public static final String STR_MESSAGE = "字符串不允许为空！";
    public static final String INT_MESSAGE = "数字不允许为空！";

    public static void isNullOrEmpty(Object obj, String message) throws CheckException {
        if(obj == null){
            throw new CheckException(message);
        }else{
            Boolean flag = false;
            try {
                Method method = obj.getClass().getMethod("isEmpty");
                flag = (Boolean)method.invoke(obj);
            }catch (Exception e){
                throw new CheckException("Parameter type is not set.");
            }
            if(flag){
                throw new CheckException(message);
            }
        }
    }

    public static void isBlank(String str) throws CheckException {
        isBlank(str,STR_MESSAGE);
    }

    public static void isBlank(String str,String message) throws CheckException {
        if(StringUtils.isBlank(str)){
            throw new CheckException(message);
        }
    }

    public static void isNotBlank(String str,String message) throws CheckException {
        if(StringUtils.isNotBlank(str)){
            throw new CheckException(message);
        }
    }

    public static void isBlank(CharSequence... str) throws CheckException {
        isBlank(STR_MESSAGE,str);
    }

    public static void isBlank(String message,CharSequence... str) throws CheckException {
        if(StringUtils.isAllBlank(str)){
            throw new CheckException(message);
        }
    }

    public static void isNotBlank(String message,CharSequence... str) throws CheckException {
        if(StringUtils.isNoneBlank(str)){
            throw new CheckException(message);
        }
    }

    public static void isNull(Integer integer) throws CheckException {
        isNull(integer,INT_MESSAGE);
    }

    public static void isNull(Object obj,String message) throws CheckException {
        if(obj == null){
            throw new CheckException(message);
        }
    }

    public static void isNotNull(Object obj,String message) throws CheckException {
        if(obj != null){
            throw new CheckException(message);
        }
    }

    public static void isNull(Integer integer,String message) throws CheckException {
        if(integer == null){
            throw new CheckException(message);
        }
    }
    public static void isNull(List list,String message) throws CheckException {
        if(CollectionUtils.isEmpty(list)){
            throw new CheckException(message);
        }
    }

    public static void isNotNull(List list,String message) throws CheckException {
        if(!CollectionUtils.isEmpty(list)){
            throw new CheckException(message);
        }
    }

    /*
    *重复校验
    * */
    public static void isRepeat(List list, String message) throws CheckException {
        if(list !=null && list.size()>0){
            throw new CheckException(message);
        }
    }
    public static void isRepeat(List list, Set set, String message) throws CheckException {
        if(list.size() > set.size()){
            throw new CheckException(message);
        }
    }

    /*
    *不同校验
    * */
    public static void isNotEquals(Object oldObj,Object newObj , String message) throws CheckException {
        if(!oldObj.equals(newObj)){
            throw new CheckException(message);
        }
    }

}
